package com.hafiery.sdk.bluetooth.test.adapter;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.hafiery.sdk.bluetooth.test.R;

import java.util.List;
import java.util.Set;

public class BleListAdapter extends RecyclerView.Adapter<BleListAdapter.MyViewHolder> {
    private Set<BluetoothDevice> mDatas;
    private OnItemClicklistener onItemClicklistener;
    public BleListAdapter(Set<BluetoothDevice> mDatas){
        this.mDatas=mDatas;
    }

    public void setOnItemClicklistener(OnItemClicklistener onItemClicklistener) {
        this.onItemClicklistener = onItemClicklistener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_item_blelist,null,false);
        MyViewHolder myViewHolder=new MyViewHolder(itemView);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        BluetoothDevice item=mDatas.toArray(new BluetoothDevice[]{})[position];
        holder.itemView.setOnClickListener(v -> {
            if(onItemClicklistener!=null)
                onItemClicklistener.onItemClick(item,position);
        });
        TextView bleNameTv=holder.itemView.findViewById(R.id.bleNameTv);
        TextView macTv=holder.itemView.findViewById(R.id.macTv);
        TextView rssiTv=holder.itemView.findViewById(R.id.rssiTv);
        bleNameTv.setText(item.getName());
        macTv.setText(item.getAddress());
//        rssiTv.setText(item.rssi+"");
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClicklistener{
        void onItemClick(BluetoothDevice item,int position);
    }
}

